<template>
  <div class="sale-hotel">
    <el-breadcrumb>
      <el-breadcrumb-item>系统设置</el-breadcrumb-item>
      <el-breadcrumb-item>酒店列表</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="sale-hotel-content">
      <el-form>
        <el-form-item>
          <el-dropdown trigger="click"
            @command="dropdownStateChange">
            <span class="el-dropdown-link">
              {{dropdownValue}}
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item :command="{value: null, title: '全部状态'}">
                全部状态
              </el-dropdown-item>
              <el-dropdown-item :command="{value: 0, title: '启用'}">
                启用
              </el-dropdown-item>
              <el-dropdown-item :command="{value: 1, title: '禁用'}">
                禁用
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <div class="area-select">
            <el-cascader :options="cities"
              :props="{ value: 'cityid', label: 'city', children: 'citiess' }"
              @change="handleCascader">
            </el-cascader>
          </div>
          <div class="input-with-select el-input el-input-group el-input-group--append">
            <input autocomplete="off"
              placeholder="请输入酒店名/酒店ID"
              type="text"
              rows="2"
              validateevent="true"
              class="el-input__inner"
              v-model="searchKey"
              @keyup.enter="searchHotel">

          </div>
          <el-button type="primary"
            @click="searchHotel"
            class="searchHotel">
            <i class="el-icon-search"></i>
          </el-button>
          <!-- <el-button type="primary"
                        size="small"
                        style="float: right; margin-right: 1%;"
                        @click="addSaleHotel"
                        v-has="'createhotel'">新增酒店</el-button> -->
        </el-form-item>
        <div class="hotel-state">
          <el-form-item>
            <el-popover ref="hotelState"
              placement="bottom"
              width="200"
              data-hotel="state"
              v-model="visible"
              trigger="click">
              <el-form-item>
                <div class="title">
                  <h1>设置酒店状态</h1>
                </div>
                <div class="radio-style">
                  <div class="radio-left"
                    style="text-align:center">
                    <el-radio v-model="statusBtn"
                      :label="0">启用</el-radio>
                    <el-radio v-model="statusBtn"
                      :label="1">停用</el-radio>
                  </div>
                </div>
                <div class="popver-btn">
                  <el-button type="primary"
                    @click="batchState"
                    style="color:#fff">确定</el-button>
                  <el-button @click="visible = false">取消</el-button>
                </div>
              </el-form-item>
            </el-popover>
            <el-button @click="setHotelState"
              v-has="'hotelstatus'">设置酒店状态</el-button>
          </el-form-item>
        </div>
        <el-form-item>
          <el-table :data="tableData"
            @selection-change="handleSelectionChange"
            max-height="600">
            <el-table-column type="index">
            </el-table-column>
            <el-table-column type="selection">
            </el-table-column>
            <el-table-column prop="num"
              label="酒店ID">
            </el-table-column>
            <el-table-column prop="name"
              label="酒店名称">
            </el-table-column>
            <el-table-column prop="province"
              label="所在省份">
            </el-table-column>
            <el-table-column prop="city"
              label="所在城市">
            </el-table-column>
            <el-table-column prop="area"
              label="所在区/县">
            </el-table-column>
            <el-table-column prop="star"
              label="星级">
            </el-table-column>
            <el-table-column prop="status"
              label="状态">
            </el-table-column>
            <el-table-column label="操作"
              width="120">
              <template slot-scope="scope">
                <el-button @click.native.prevent="stateRow(scope.$index, tableData)"
                  type="text"
                  size="small"
                  v-has="'hotelstatus'">
                  {{(scope.$index, state[scope.$index])}}
                </el-button>
                <el-button v-has="'saleroom'"
                  type="text"
                  v-if="((scope.$index, state[scope.$index]) === '启用')"
                  disabled
                  size="small">
                  房型
                </el-button>
                <el-button v-has="'saleroom'"
                  @click.native.prevent="jumpRoom(tableData[scope.$index].id)"
                  type="text"
                  v-if="((scope.$index, state[scope.$index]) === '停用')"
                  size="small">
                  房型
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item style="margin-top: 20px;">
          <el-pagination @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="total, prev , pager , next, sizes, jumper"
            :total="total">
          </el-pagination>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import {
  getSaleHotelList,
  setSaleHotelState
} from "../../../public/js/saleHotel";
const Breadcrumb = () => import("@/components/common/Breadcrumb");
const cityData = require("@/data/cities.json");
export default {
  name: "SaleHotellist",
  data() {
    return {
      state: [],
      status: 0,
      dropdownValue: "全部状态",
      cities: cityData.provincesEntities,
      visible: false,
      content: {
        firstTitle: "酒店",
        secondTitle: "酒店列表",
        type: "close"
      },
      tableData: [],
      selectionArray: [],
      total: 0,
      saleHotelIds: [],
      statusSelect: null,
      statusBtn: null,
      pageSize: 10,
      pageNum: 1,
      city: "",
      searchKey: "",
      text: ""
    };
  },
  created() {
    this.getSaleHotelLists();
  },
  methods: {
    getSaleHotelLists() {
      getSaleHotelList(this).then(res => {
        if (res.data.errCode === 0) {
          this.total = res.data.total;
          this.tableData = res.data.list;
          this.stateData();
        }
      });
    },
    setSaleHotelStates() {
      setSaleHotelState(this).then(res => {
        if (res.data.errCode === 0) {
          this.total = res.data.total;
          this.tableData = res.data.list;
          this.$message.success("修改成功");
          this.saleHotelIds = [];
          this.statusBtn = "";
          this.stateData();
        }
      });
    },
    stateData() {
      this.state = [];
      this.tableData.forEach((value, index) => {
        if (value.status === "停用") {
          this.state.push("启用");
        } else {
          this.state.push("停用");
        }
      });
    },
    setHotelState() {
      if (!this.saleHotelIds.length) {
        this.$message.error("请勾选要修改的酒店");
        return false;
      }
      this.visible = true;
    },
    jumpRoom(val) {
      window.bus.$emit("hotelId", val);
      this.$router.push("/index/room_management");
    },
    batchState() {
      this.setSaleHotelStates();
      this.visible = false;
    },
    handleCascader(val) {
      this.pageNum = 1;
      this.city = parseInt(val[val.length - 1]);

      this.getSaleHotelLists();
    },
    searchHotel() {
      this.pageNum = 1;
      this.getSaleHotelLists();
    },
    stateRow(index, rows) {
      this.saleHotelIds = [];
      this.saleHotelIds.push(this.tableData[index].id);
      this.tableData[index].status === "启用"
        ? (this.statusBtn = 1)
        : (this.statusBtn = 0);
      this.tableData[index].status === "启用"
        ? (this.text = "停用")
        : (this.text = "启用");
      this.$confirm("此操作将" + this.text + "该酒店, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.setSaleHotelStates();
        })
        .catch(() => {});
    },
    dropdownStateChange(data) {
      this.pageNum = 1;
      this.statusSelect = data.value;
      this.getSaleHotelLists();
      this.dropdownValue = data.title;
    },
    addSaleHotel() {
      this.$router.push("/index/add_sale_hotel");
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getSaleHotelLists();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getSaleHotelLists();
    },
    handleSelectionChange(row) {
      this.saleHotelIds = [];
      this.selectionArray = row;
      this.selectionArray.forEach((item, index) => {
        this.saleHotelIds.push(item.id);
      });
      console.log(this.saleHotelIds);
    }
  },
  components: {
    Breadcrumb
  }
};
</script>
<style lang="scss" scoped>
.sale-hotel-content {
  background-color: white;
  padding: 20px;
  position: relative;
  .el-form {
    .el-form-item {
      .el-form-item__content {
        line-height: 34px !important;
        .el-dropdown {
          float: left;
          margin-right: 20px;
        }
        .el-input {
          float: left;
          width: 300px;
          input {
            height: 34px;
            line-height: 34px;
          }
        }
        .el-pagination {
          text-align: right;
          .el-pagination__jump {
            .el-input {
              float: none !important;
              width: 50px !important;
            }
          }
          .el-input {
            width: 100px;
          }
        }
        .el-table__header,
        .el-table__body {
          width: 100% !important;
        }
      }
    }
  }
}
.expand-trigger div {
  height: 50px !important;
}
.hotel-state {
  text-align: left;
  .el-button {
    font-size: 12px;
    color: #6d2377;
    border: 1px solid #6d2377;
    padding: 0 20px;
    line-height: 32px;
  }
}
.area-select {
  float: left;
  line-height: 1;
  width: 200px;
  margin-right: 20px;
  .el-cascader {
    line-height: 1;
    width: 200px;
    .el-input {
      width: 200px !important;
    }
  }
  .el-cascader .el-icon-arrow-down {
    line-height: 1;
  }
}
.searchHotel {
  float: left;
  margin-left: 15px;
  padding: 9px 21px;
}
.el-popper[x-placement^="bottom"] {
  width: 100px !important;
  padding: 0;
}
.el-popper[x-placement^="bottom"] .el-form-item .el-form-item__content .title {
  background-color: rgb(241, 243, 248);
  text-align: left;
  padding-left: 20px;
  h1 {
    font-size: 14px;
  }
}
.el-popper[x-placement^="bottom"]
  .el-form-item
  .el-form-item__content
  .radio-style {
  padding: 20px;
  text-align: center;
  overflow: hidden;
  .radio-left,
  .radio-right {
    float: left;
    width: 50%;
  }
}
.popver-btn {
  text-align: center;
  padding-top: 20px;
  button {
    line-height: 32px;
    padding: 0 20px;
  }
}
</style>
